<template>
  <div>
    <el-dialog center :title="isAdd ? '添加轮播图' : '编辑轮播图'" :visible.sync="isShow" :close-on-press-escape="false"
      :before-close="offCheck">
      <el-form :model="bannerInfo" :rules="rules" ref="bannerForm">
        <el-form-item prop="title" label="标题" :label-width="formLabelWidth">
          <el-input v-model="bannerInfo.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图片" :label-width="formLabelWidth">
          <!-- 图片上传组件 -->
          <el-upload action="#" list-type="picture-card" :on-preview="onPreview" :on-remove="onRemove"
            :on-change="changeFile" :file-list="fileList" :limit="1" :on-exceed="handleExceed">
            <i class="el-icon-plus"></i>
          </el-upload>
          <!-- 图片预览组件 -->
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch v-model="bannerInfo.status" active-color="#13ce66" inactive-color="#ccc" :active-value="1"
            :inactive-value="2">
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="offCheck">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 引入接口地址
import { bannerAdd, bannerInfo, bannerEdit } from '@/request/api';
// 引入辅助性函数
import { mapActions } from 'vuex';
export default {
  props: ["isShow", "isAdd"],
  data() {
    return {
      bannerInfo: {
        title: "",
        img: "",
        status: 1,
      },
      formLabelWidth: "120px",
      rules: {
        title: [
          { required: true, message: "请输入轮播图名称", trigger: "blur" },
          { min: 2, max: 12, message: "长度在 2 到 12 个字符", trigger: "blur" },
        ],
      },
      dialogVisible: false,
      dialogImageUrl: "",
      fileList: [],
    };
  },
  methods: {
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length
        } 个文件`
      );
    },
    // 文件修改时触发
    changeFile(file, filelist) {
      this.bannerInfo.img = file.raw;
    },
    // 图片预览
    onPreview(file) {
      //给预览图片赋值
      this.dialogImageUrl = file.url;
      //打开弹框
      this.dialogVisible = true;
    },
    // 图片删除
    onRemove(file, filelist) {
    },

    // 点击取消
    offCheck() {
      // 子传父
      this.$emit("offCheck", false)
      // 清空表单信息
      this.$refs.bannerForm.resetFields()
      this.bannerInfo = {
        title: "",
        img: "",
        status: 1,
      }
      this.fileList = []
    },
    // 获取轮播图列表
    ...mapActions({
      getBannerList: "banner/getBannerList_Action"
    }),
    // 商品分类数据回显
    getOneCate(id) {
      // 调用查询一条轮播图信息接口
      bannerInfo({ id }).then(res => {
        console.log(res);
        if (res.code == 200) {
          // 进行数据回显
          this.bannerInfo = res.list
          this.bannerInfo.id = id
          this.fileList = [{ url: this.$imgUrl + res.list.img }]
        }
      })
    },
    //点击确定
    addCate() {
      this.$refs.bannerForm.validate((flag) => {
        if (flag) {
          //判断是否为添加还是编辑
          if (this.isAdd) {
            //调用添加接口
            bannerAdd(this.bannerInfo).then(res => {
              if (res.code == 200) {
                // 重新渲染轮播图列表
                this.getBannerList()
                this.offCheck()
              }
            })
          } else {
            //调用编辑接口
            bannerEdit(this.bannerInfo).then(res => {
              if (res.code == 200) {
                // 重新渲染轮播图列表
                this.getBannerList()
                this.offCheck()
              }
            })
          }
        } else {
          // 错误提示
          this.$message.error("请输入正确的内容");
        }
      });
    },
  },
};
</script>

<style scoped>
</style>
